package com.safone.thirdattendanceservices.schoolgate.services.impl;

import com.alibaba.fastjson.JSON;
import com.safone.thirdattendanceservices.configs.ArgsUtil;
import com.safone.thirdattendanceservices.schoolgate.services.SchoolGateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author xy
 * @date 2020/12/4 20:47
 */
@Service
public class SchoolGateServiceImpl implements SchoolGateService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ArgsUtil argsUtil;

    private Boolean key = true;

    /**
     * 定时任务：
     *     每日晚12点获取当日考勤数据，并推送到kafka
     */
//    @Scheduled(cron = "55 59 23 * * ?")
    public void saveSchoolGateAttendance() {
        //储存字典
        if (key) {
            String prefixUrl =
                    "http://localhost:13000/api/safe/aggregation/externalData/pushDataDictionary";
            // 设置请求header信息
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.add("Accept", MediaType.ALL_VALUE);
            headers.add(
                    "Authorization",
                    "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI3MzMyIiwiY29kZSI6InNhZmUtc2Nob29sIiwiZXhwIjoxNjA4MTg5NTE2LCJpYXQiOjE2MDgxMDMxMTZ9.apf1zcfBqgR8uLYgdzt9yFoz4Jad2R6RW9rBzbszSNF0RGoM4P9GU5k5MXdy8ynA4rLZU_da29e9emS6K9s9yg");
            // 设置请求参数
            Map params = new HashMap<>();
            params.put("type", "schoolGateAttendance");
      String data =
          "{\"stuId\":\"人员id\",\"normalIn\":\"正常入\",\"late\":\"迟到\",\"notCome\":\"未上学\",\"normalOut\":\"正常出\",\"early\":\"早退\",\"notLeave\":\"未离校\"，\"holiday\":\"请假\"，\"code\":\"学工号\"，\"name\":\"姓名\"，\"sex\":\"性别\"，\"className\":\"班级名称\"}";
            params.put("data", data);
            // 封装请求头和内容
            HttpEntity<Map> requestEntity = new HttpEntity<>(params, headers);
            // 发送请求
            ResponseEntity<String> stringResponseEntity =
                    restTemplate.postForEntity(prefixUrl, requestEntity, String.class);
            key = false;
        }
        //推送数据
        Map data =(Map) getAllSchoolGateAttendance().getBody().get("data");
        List list = (List<Map<String,Object>>)data.get("list");
        String prefixUrl = "http://localhost:13000/api/safe/aggregation/externalData/pushData";
        // 设置请求header信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Accept", MediaType.ALL_VALUE);
        headers.add(
                "Authorization",
                "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI3MzMyIiwiY29kZSI6InNhZmUtc2Nob29sIiwiZXhwIjoxNjA4MTg5NTE2LCJpYXQiOjE2MDgxMDMxMTZ9.apf1zcfBqgR8uLYgdzt9yFoz4Jad2R6RW9rBzbszSNF0RGoM4P9GU5k5MXdy8ynA4rLZU_da29e9emS6K9s9yg");
        // 设置请求参数
        Map params = new HashMap();
        params.put("type", "schoolGateAttendance");
        params.put("typeDescribe", "出入口考勤统计数据");
        params.put("data", JSON.toJSONString(list));
        // 封装请求头和内容
        HttpEntity<Map> requestEntity = new HttpEntity<>(params, headers);
        // 发送请求
        ResponseEntity<String> stringResponseEntity =
                restTemplate.postForEntity(prefixUrl, requestEntity, String.class);
    }


    @Override
    public ResponseEntity<Map> getAllSchoolGateAttendance() {
        int pageNum=1;
        int pageSize=10;
        String searchStartDate="2019-01-01";
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String searchEndDate=format.format(date);
        String orgIds="";
        String keyWord="";
        String apiUrl = "/eags/api/fsds/v1.0/attendance/stus"
                +"?pageNum="+pageNum
                +"&pageSize="+pageSize
                +"&searchStartDate="+searchStartDate
                +"&searchEndDate="+searchEndDate
                +"&orgIds="+orgIds
                +"&keyWord="+keyWord;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<Map> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, Map.class);
        return responseEntity;
    }

    @Override
    public Object getOneSchoolGateAttendance() {
        int pageNum=1;
        int pageSize=10;
        String searchStartDate="1990-01-01";
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String searchEndDate=format.format(date);
        Long stuId=1L;
        String apiUrl = "/eags/api/fsds/v1.0/attendance/stuId"
                +"?pageNum="+pageNum
                +"&pageSize="+pageSize
                +"&searchStartDate="+searchStartDate
                +"&searchEndDate="+searchEndDate
                +"&stuId="+stuId;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<String> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, String.class);
//        JSONObject data = JSONObject.parseObject(responseEntity.getBody()).getJSONObject("data");
        return responseEntity;
    }

    @Override
    public Object getVisitorDetailById() {
        Long id=1L;
        String apiUrl = "/eags/api/svms/v1.0/visitor/detail"
                +"?id="+id;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<String> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, String.class);
//        JSONObject data = JSONObject.parseObject(responseEntity.getBody()).getJSONObject("data");
        return responseEntity;
    }

    @Override
    public Object getVisitorByPage() {
        String pageNum="1";
        String pageSize="10";
        String name="";
        String workplace="";
        String isvName="";
        String isvCode="";
        int Status=1;
        String comTimeStr="";
        String backTimeStr="";
        String phone="";
        String apiUrl = "/eags/api/svms/v1.0/visitor"
                +"?pageNum="+pageNum
                +"&pageSize="+pageSize
                +"&name="+name
                +"&workplace="+workplace
                +"&isvName="+isvName
                +"&isvCode="+isvCode
                +"&Status="+Status
                +"&comTimeStr="+comTimeStr
                +"&backTimeStr="+backTimeStr
                +"&phone="+phone;
        String httpMethod = "GET";
        argsUtil.restTemplateExchange(apiUrl,httpMethod,null);
        HttpHeaders headers = argsUtil.getHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(null,headers );
        ResponseEntity<String> responseEntity = restTemplate.exchange(argsUtil.getDaHuaConfig().getUrl()+apiUrl, HttpMethod.GET, formEntity, String.class);
//        JSONObject data = JSONObject.parseObject(responseEntity.getBody()).getJSONObject("data");
        return responseEntity;
    }

}


